package news.dao;

import news.example.Publish;
import news.example.Topic;
import news.util.BaseDao;

import java.util.ArrayList;
import java.util.List;

/**
 * @author JiangJ
 * @date 2022/3/2 20:37
 */
public class SelTopicImpl implements ISelTopicDao {
    BaseDao baseDao = new BaseDao();
    List<Topic> list = null;

    /**
     * 查询
     *
     * @return
     */
    @Override
    public List<Topic> findAllTopic() {

        try {
            baseDao.openDB();
            String sql = "select * from news_topic";
            baseDao.st = baseDao.conn.createStatement();
            baseDao.rs = baseDao.st.executeQuery(sql);
            list = new ArrayList<Topic>();

            while (baseDao.rs.next()) {
                Topic topic = new Topic();
                topic.setId(baseDao.rs.getInt(1));
                topic.setTopicName(baseDao.rs.getString(2));
                topic.setCreateTime(baseDao.rs.getDate(3));
                list.add(topic);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Override
    public List<Topic> findNameById(Integer id) {
        try {
            baseDao.openDB();
            String sql = "select topicname from news_topic where id = ?";
            baseDao.ps = baseDao.conn.prepareStatement(sql);
            baseDao.ps.setInt(1, id);
            baseDao.rs = baseDao.ps.executeQuery();
            list = new ArrayList<Topic>();
            while (baseDao.rs.next()) {
                Topic topic = new Topic();
                topic.setTopicName(baseDao.rs.getString("topicname"));
                list.add(topic);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<Topic> getTopicName() {
        try {
            baseDao.openDB();
            String sql = "select id,topicname from news_topic";
            baseDao.ps = baseDao.conn.prepareStatement(sql);
            baseDao.rs = baseDao.ps.executeQuery();
            list = new ArrayList<Topic>();
            while (baseDao.rs.next()) {
                Topic topic = new Topic();
                topic.setId(baseDao.rs.getInt("id"));
                topic.setTopicName(baseDao.rs.getString("topicname"));
                list.add(topic);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

}
